var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "mobile/services/shoppingCartService", "react", "common", "components/common/component", "components/common/base-component", "services/local-service"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const shoppingCartService_1 = require("mobile/services/shoppingCartService");
    const React = require("react");
    const common_1 = require("common");
    const component_1 = require("components/common/component");
    const base_component_1 = require("components/common/base-component");
    const local_service_1 = require("services/local-service");
    exports.MenuStorageName = "Menu";
    exports.defaultShowIcon = false;
    const EMPTY_ICON = 'icon-check-empty';
    let Menu = class Menu extends base_component_1.BaseComponent {
        constructor(props) {
            super(props);
            this.localService = this.createService(local_service_1.LocalService);
            let productsCount = shoppingCartService_1.ShoppingCartService.productsCount.value;
            this.state = { productsCount, menuNodes: [] };
        }
        componentDidMount() {
            return __awaiter(this, void 0, void 0, function* () {
                let { menuNodes, showIcon } = (yield this.localService.getDataObject(exports.MenuStorageName)) || {};
                menuNodes = menuNodes || [];
                this.setState({ menuNodes, showIcon });
                common_1.subscribe(this, shoppingCartService_1.ShoppingCartService.productsCount, (value) => {
                    this.setState({ productsCount: value });
                });
            });
        }
        render() {
            // let { showIcon } = this.props;
            let { menuNodes, showIcon } = this.state;
            showIcon = showIcon == null ? exports.defaultShowIcon : showIcon;
            menuNodes.sort((a, b) => {
                return (a.sortNumber || 0) - (b.sortNumber || 0);
            });
            return React.createElement("div", { key: "menuControl", className: "menuControl" }, menuNodes.length <= 0 ?
                React.createElement("ul", { className: "menu noicon" }) :
                showIcon ? this.renderMenuWithIcon(menuNodes) : this.renderMenuWithoutIcon(menuNodes));
        }
        renderMenuWithoutIcon(menuNodes) {
            return (React.createElement("ul", { className: "menu noicon" }, menuNodes.map((o, i) => {
                let itemWidth = 100 / menuNodes.length;
                return (React.createElement("li", { key: i, style: { width: `${itemWidth}%` } },
                    React.createElement("a", { href: o.url }, o.name)));
            })));
        }
        renderMenuWithIcon(menuNodes) {
            let productsCount = this.state.productsCount || 0;
            // return <PageContext.Consumer>{
            //     args => {
            return React.createElement("ul", { className: "menu" }, menuNodes.map((o, i) => {
                let itemWidth = 100 / menuNodes.length;
                // let routeString = o.url.startsWith('#') ? o.url.substr(1) : o.url;
                let page = this.page; //(this.elementPage as UserPage);
                var routeData = o.url ? page.app.parseUrl(o.url) : { pageName: "" };
                let isShoppingCart = routeData.pageName == 'shopping/shoppingCart';
                let isActive = page.name == routeData.pageName; //app.currentPage != null && app.currentPage.name == routeData.pageName;
                return (React.createElement("li", { key: i, style: { width: `${itemWidth}%` } },
                    React.createElement("div", { onClick: () => page.app.redirect(routeData.pageName), className: isActive ? 'text-primary' : null },
                        React.createElement("i", { className: o.icon ? o.icon : EMPTY_ICON }),
                        o.name),
                    isShoppingCart && productsCount > 0 ? React.createElement("div", { className: "badge bg-primary" }, productsCount) : null));
            }));
            //     }
            // }</PageContext.Consumer>
        }
    };
    Menu = __decorate([
        component_1.component()
    ], Menu);
    exports.Menu = Menu;
});
